// لله درك
#pragma GCC optimize ("O3")
#pragma GCC optimization("Ofast")
#pragma GCC target("avx2")
#pragma GCC optimization("unroll-loops")
#include <bits/extc++.h>
#define Caraxes ios_base :: sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define int long long int
#define F first
#define S second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define inf 1e18
using namespace std;
template< typename T = int >
std :: vector < T > get(int size = 0, std :: istream& in_stream = std :: cin);
int calc(string a, string b){
if(a == b)
return 0;
if(sz(a) != sz(b))
return -1;
int cnt = 0;
string tmp1 = a, tmp2 = b;
sort(all(a));
sort(all(b));
for(int i = 0;i < sz(a); ++i){
if(a[i] != b[i])
return -1;
}
a = tmp1;
b = tmp2;
while(b != a){
b.push_back(b.front());
b.erase(b.begin());
cnt++;
if(cnt && b == tmp2)
return -1;
}
return cnt;
}
void Dracarys() {
int n;
cin >> n;
vector < string > v(n);
for(int i = 0;i < n; ++i){
cin >> v[i];
}
int ans = INT_MAX;
for(int i = 0;i < n; ++i){
int cnt = 0;
for(int j = 0;j < n; ++j){
int z = calc(v[i],v[j]);
if(z == -1){
return cout << -1,void();
}
cnt+= z;
}
ans = min(ans,cnt);
}
cout << ans << '\n';
}
signed main()
{
Caraxes
#ifndef ONLINE_JUDGE
::freopen("in.txt","r",stdin);
::freopen("out.txt","w",stdout);
#endif
int test = 1;
//cin >> test;
while(test--) {
Dracarys();
}
}
template< typename T>
std :: vector < T > get(int size , std :: istream& in_stream){
if(!size){
in_stream >> size;
}
std :: vector < T > array(size);
for(auto& i : array)
in_stream >> i;
return array;
}
344. Reverse String | 1047. Remove All Adjacent Duplicates In String |
977. Squares of a Sorted Array | 852. Peak Index in a Mountain Array |
461. Hamming Distance | 1748. Sum of Unique Elements |
897. Increasing Order Search Tree | 905. Sort Array By Parity |
1351. Count Negative Numbers in a Sorted Matrix | 617. Merge Two Binary Trees |
1450. Number of Students Doing Homework at a Given Time | 700. Search in a Binary Search Tree |
590. N-ary Tree Postorder Traversal | 589. N-ary Tree Preorder Traversal |
1299. Replace Elements with Greatest Element on Right Side | 1768. Merge Strings Alternately |
561. Array Partition I | 1374. Generate a String With Characters That Have Odd Counts |
1822. Sign of the Product of an Array | 1464. Maximum Product of Two Elements in an Array |
1323. Maximum 69 Number | 832. Flipping an Image |
1295. Find Numbers with Even Number of Digits | 1704. Determine if String Halves Are Alike |
1732. Find the Highest Altitude | 709. To Lower Case |
1688. Count of Matches in Tournament | 1684. Count the Number of Consistent Strings |
1588. Sum of All Odd Length Subarrays | 1662. Check If Two String Arrays are Equivalent |